package com.et114.modules.myet.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.components.validCode.AuthCodeImg;
import com.et114.core.common.AppContext;
import com.et114.modules.cust.form.Mng_CustForm;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.myet.service.IMyEt_UCenterService;
import com.et114.modules.myet.service.MyEt_UCenterServiceImp;
import com.et114.modules.products.commons.PorJsonObj;
import com.et114.modules.suspect.service.IMng_SuspectService;
import com.et114.modules.suspect.service.Mng_SuspectServiceImp;
import com.et114.modules.suspect.vo.SusPectVO;

/**
 * 个人信息管理
 * @author zhiweip
 * @version 1.0
 * @created 22-十二月-2010 13:33:57
 */
public class Et_UserAction extends MyEtAction {
	private IMng_CustService	mng_CustService	= ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
	private IMyEt_UCenterService	m_IMyEt_UCenterService	= ( MyEt_UCenterServiceImp ) getBean ( "m_IMyEt_UCenterService" );
	
	public Et_UserAction(){

	}

	public void finalize() throws Throwable {

	}

	/**
	 * 个人基本信息
	 */
	public ActionForward unspecified ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		if ( isNeedLogin ( request )) {return mapping.findForward ( "et_info" );}
		
		Mng_CustForm cForm=(Mng_CustForm)form;
		CustBaseVo cust = new CustBaseVo ( );
		cust.setCustid (   super.getCustId ( request ) ); 
		request.setAttribute ( "BASEINFO" , mng_CustService
				.loadCust ( cust ) );
		return mapping.findForward ("custinfo");
	}
	public ActionForward toLoad ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		if ( isNeedLogin ( request )) {return mapping.findForward ( "et_info" );}
		
		Mng_CustForm cForm=(Mng_CustForm)form;
		CustBaseVo cust = new CustBaseVo ( );
		cust.setCustid (   super.getCustId ( request ) ); 
		request.setAttribute ( "BASEINFO" , mng_CustService
				.loadCust ( cust ) ); 
		return mapping.findForward ("custinfo_ajax");
	}
	
	public ActionForward udpBaseInfo ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		if ( isNeedLogin ( request )) {return mapping.findForward ( "et_info" );}
		
		Mng_CustForm cForm=(Mng_CustForm)form;
		CustBaseVo cust = cForm.getCust ( );
		cust.setCustid (   super.getCustId ( request ) ); 

		m_IMyEt_UCenterService.updateMyInfo ( cust );
		
		request.setAttribute ( "BASEINFO" , mng_CustService
				.loadCust ( cust ) );
		return mapping.findForward ("custinfo");
	}
	
	public ActionForward toudpBaseInfo ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		logger.debug ( "==========updateInfo========rr======key=:" );
		if ( isNeedLogin ( request )) {return mapping.findForward ( "et_info" );}
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		Mng_CustForm cForm=(Mng_CustForm)form;
		CustBaseVo cust = cForm.getCust ( );
		cust.setCustid (   super.getCustId ( request ) );
		
		String custname=request.getParameter ( "custname" );
		if(custname!=null)
			custname=new String(custname.getBytes("ISO8859-1"),"UTF-8");
		String famname=request.getParameter ( "famname" );
		if(famname!=null)
			famname=new String(famname.getBytes("ISO8859-1"),"UTF-8");
		String nickname=request.getParameter ( "nickname" );
		if(nickname!=null)
			nickname=new String(nickname.getBytes("ISO8859-1"),"UTF-8");
		String birth=request.getParameter ( "birth" );
		String address=request.getParameter ( "addressid" );
		if(address!=null)
			address=new String(address.getBytes("ISO8859-1"),"UTF-8");
		
		logger.debug ( "==========updateInfo==============key=:" + address );
		String city=request.getParameter ( "cityid" );
		if(city!=null)
			city=new String(city.getBytes("ISO8859-1"),"UTF-8");
		String state=request.getParameter ( "stateid" );
		if(state!=null)
			state=new String(state.getBytes("ISO8859-1"),"UTF-8");
		
		String safeno=request.getParameter ( "safeno" );
		String email=request.getParameter ( "email" );
		if(email!=null)
			email=new String(email.getBytes("ISO8859-1"),"UTF-8");
		String mobileno=request.getParameter ( "mobileno" );
		String othertel=request.getParameter ( "othertel" );
		String sex=request.getParameter ( "sex" );
		if(sex!=null)
			sex=new String(sex.getBytes("ISO8859-1"),"UTF-8");
		
		
		cust.setCustname ( custname );
		cust.setFamname ( famname );
		cust.setNickname ( nickname );
		cust.setBirth ( birth );
		cust.setAddress ( address );
		cust.setCity ( city );
		cust.setState ( state );
		cust.setSafeno ( safeno );
		cust.setEmail ( email );
		cust.setMobileno ( mobileno );
		cust.setOthertel ( othertel );
		cust.setSex ( sex );

		m_IMyEt_UCenterService.updateMyInfo ( cust );
		
		HashMap optMap=new HashMap();
		JSONArray comjson = JSONArray.fromObject ( optMap );
		
		String result = comjson.toString ( );
		
		request.setAttribute ( "msg" , "ok" );
		response.getWriter ( ).flush ( );
		response.getWriter ( ).close ( );
		return null;
	}

	/**
	 * 密码修改
	 */
	public void myPWD(){

	}

	public ActionForward  rfq ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		 
		return mapping.findForward ("input");
	}
	
	public ActionForward  rfqInput ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
	throws Exception {
		String title=request.getParameter ( "rfqtitle" );
		String content=request.getParameter ( "rfqcontent" );
		String username=request.getParameter ( "username" );
		String usertel=request.getParameter ( "usertel" );
		String calltime=request.getParameter ( "calltime" );
		String stype=request.getParameter ( "stype" );
		String vcode=request.getParameter ( "checkcode" );
		SusPectVO sus=new SusPectVO();
		sus.setSporderid ( title );
		sus.setSpcontent ( content );
		sus.setStype ( stype );
		sus.setUsername ( username );
		sus.setUsertel ( usertel );
		sus.setNextdate ( calltime );
		
		HttpSession session = request.getSession ( );
		
		if ( session.getAttribute ( AuthCodeImg.ACEGI_SECURITY_FORM_VALIDCODE_KEY )==null ||! session.getAttribute (
				AuthCodeImg.ACEGI_SECURITY_FORM_VALIDCODE_KEY ).equals ( vcode ) ){
			request.setAttribute ( "checkcode" , "error" );
			request.setAttribute ( "SusPect" , sus );
			return mapping.findForward ("input");
		}
		session.removeAttribute ( AuthCodeImg.ACEGI_SECURITY_FORM_VALIDCODE_KEY );
		
		IMng_SuspectService	m_IMng_SuspectService	= ( Mng_SuspectServiceImp ) getBean ( "m_IMng_SuspectService" );
	
		sus.setSpresult ( "0" );//需跟进
		m_IMng_SuspectService.insertSuspect ( sus );
		request.setAttribute ( "result" , "error" );
		request.setAttribute ( "result" , "rfqok");
		return mapping.findForward ("et_info");
	}

}